package com.zhouyibike.uu.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.entity.Cityinfo;
import com.zhouyibike.uu.entity.Threecity;
import com.zhouyibike.uu.ui.adapter.Cityo1Adapter;
import com.zhouyibike.uu.ui.adapter.CityoAdapter;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.FileUtil;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class CityoActivity extends BaseActivity {
    private ListView citylv;
//    private String area_str;
//    private City.Area0Bean sheng;
//    private City.Area1Bean shi;
    private List<Cityinfo> province_list = new ArrayList<Cityinfo>();
    private HashMap<String, List<Cityinfo>> city_map = new HashMap<String, List<Cityinfo>>();
    private CityoAdapter adapter1;
    private List<Cityinfo> curCity;
    private TextView mBtnTitle;
    private String shengming;
//    private ArrayList<Mycity2> allcitylist;
//    private Mycity2 Curcity;

    private Cityo1Adapter cityadapter;
    private List<Threecity> threecitylist;
    private Threecity mycity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cityo);
//        area_str = FileUtil.readAssets(this, "area.json");
//        City city=new Gson().fromJson(area_str,City.class);
//        sheng=city.getArea0();
//        shi=city.getArea1();
//        Log.e( "onCreate: ",city.getArea0().get_$110000().toString() );
//        Log.e( "onCreate: ",city.getArea1().get_$110000().size()+"" );
//        Log.e( "onCreate: ",city.getArea1().get_$110000().get(0).get(1)+"" );
        // 读取城市信息string
        //第二种
        /*******************/
//        String area_str2 = FileUtil.readAssets(this, "area2.json");
//        Type listType = new TypeToken<ArrayList<Mycity2>>(){}.getType();
//        Gson gson = new Gson();
//        allcitylist = gson.fromJson(area_str2, listType);
//        mBtnTitle= (TextView) findViewById(R.id.bar_text_title);
//        mBtnTitle.setText("选择省份");
//        citylv= (ListView) findViewById(R.id.citylv);
//        JSONParser parser = new JSONParser();
//        String area_str = FileUtil.readAssets(this, "area.json");
//        province_list = parser.getJSONParserResult(area_str, "area0");
//        city_map = parser.getJSONParserResultArray(area_str, "area1");
//        adapter1=new CityoAdapter(this,allcitylist);
//        citylv.setAdapter(adapter1);
//        citylv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
////                curCity=city_map.get(province_list.get(position).getId());
//                Curcity=allcitylist.get(position);
//                shengming=(Curcity.getProvinceName());
//                if (Curcity.getCities().size()<=1)
//                {
//                    Intent intent=new Intent();
//                    intent.putExtra("cityname",shengming);
//                    setResult(1,intent);
//                    finish();
//                }else{
//                    Intent i=new Intent(CityoActivity.this,CitytActivity.class);
//                    i.putExtra("citylist", (Serializable) Curcity);
//                    startActivityForResult(i,200);
//                }
//
//            }
//        });
/*******************/
        String area_str2 = FileUtil.readAssets(this, "area4.json");
        Type listType = new TypeToken<ArrayList<Threecity>>(){}.getType();
        Gson gson = new Gson();
        threecitylist = gson.fromJson(area_str2, listType);
        mBtnTitle= (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("省份");
        citylv= (ListView) findViewById(R.id.citylv);
//        JSONParser parser = new JSONParser();
//        String area_str = FileUtil.readAssets(this, "area.json");
//        province_list = parser.getJSONParserResult(area_str, "area0");
//        city_map = parser.getJSONParserResultArray(area_str, "area1");
//        adapter1=new CityoAdapter(this,allcitylist);
        cityadapter=new Cityo1Adapter(this, (ArrayList<Threecity>) threecitylist);
        citylv.setAdapter(cityadapter);
        citylv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                curCity=city_map.get(province_list.get(position).getId());
                mycity=threecitylist.get(position);
                shengming=(mycity.getState());
//                if (mycity.getCities().size()<=1)
//                {
//                    Intent intent=new Intent();
//                    intent.putExtra("cityname",shengming);
//                    setResult(1,intent);
//                    finish();
//                }else{
                    Intent i=new Intent(CityoActivity.this,CitytActivity.class);
                    i.putExtra("citylist", (Serializable) mycity);
                    startActivityForResult(i,200);
//                }
            }
        });
    }

    public static class JSONParser {
        public ArrayList<String> province_list_code = new ArrayList<String>();
        public ArrayList<String> city_list_code = new ArrayList<String>();

        public List<Cityinfo> getJSONParserResult(String JSONString, String key) {
            List<Cityinfo> list = new ArrayList<Cityinfo>();
            JsonObject result = new JsonParser().parse(JSONString)
                    .getAsJsonObject().getAsJsonObject(key);

            Iterator<?> iterator = result.entrySet().iterator();
            while (iterator.hasNext()) {
                @SuppressWarnings("unchecked")
                Map.Entry<String, JsonElement> entry = (Map.Entry<String, JsonElement>) iterator
                        .next();
                Cityinfo cityinfo = new Cityinfo();

                cityinfo.setCity_name(entry.getValue().getAsString());
                cityinfo.setId(entry.getKey());
                province_list_code.add(entry.getKey());
                list.add(cityinfo);
            }
            return list;
        }

        public HashMap<String, List<Cityinfo>> getJSONParserResultArray(
                String JSONString, String key) {
            HashMap<String, List<Cityinfo>> hashMap = new HashMap<String, List<Cityinfo>>();
            JsonObject result = new JsonParser().parse(JSONString)
                    .getAsJsonObject().getAsJsonObject(key);

            Iterator<?> iterator = result.entrySet().iterator();
            while (iterator.hasNext()) {
                @SuppressWarnings("unchecked")
                Map.Entry<String, JsonElement> entry = (Map.Entry<String, JsonElement>) iterator
                        .next();
                List<Cityinfo> list = new ArrayList<Cityinfo>();
                JsonArray array = entry.getValue().getAsJsonArray();
                for (int i = 0; i < array.size(); i++) {
                    Cityinfo cityinfo = new Cityinfo();
                    cityinfo.setCity_name(array.get(i).getAsJsonArray().get(0)
                            .getAsString());
                    cityinfo.setId(array.get(i).getAsJsonArray().get(1)
                            .getAsString());
                    city_list_code.add(array.get(i).getAsJsonArray().get(1)
                            .getAsString());
                    list.add(cityinfo);
                }
                hashMap.put(entry.getKey(), list);
            }
            return hashMap;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==200&&resultCode==1)
        {
            Intent intent=new Intent();
            intent.putExtra("cityname",shengming+data.getStringExtra("cityname"));
            setResult(1,intent);
            finish();
        }
    }
}
